package org.clipjoint.service.impl;

import org.clipjoint.dao.GameShopDao;
import org.clipjoint.dao.impl.GameShopDaoImpl;
import org.clipjoint.entity.Game;
import org.clipjoint.entity.KeyWord;
import org.clipjoint.entity.User;
import org.clipjoint.entity.dto.GameDto;
import org.clipjoint.service.GameShopService;

import java.sql.SQLException;
import java.util.List;

public class GameShopServiceImpl implements GameShopService {

    private GameShopDao gameDao = new GameShopDaoImpl();

    private int pricestart = 0;
    private int priceend = 99999999;
    @Override
    public List<Game> getList(GameDto gameDto) throws SQLException {
        if (gameDto.getPrice() != null){
            if (1.0 == gameDto.getPrice()){
                pricestart = 0;
                priceend = 20;
            }
        }
        if (gameDto.getPrice() != null){
            if (2.0 == gameDto.getPrice()){
                pricestart = 20;
                priceend = 50;
            }
        }
        if (gameDto.getPrice() != null){
            if (3.0 == gameDto.getPrice()){
                pricestart = 50;
                priceend = 100;
            }
        }
        if (gameDto.getPrice() != null){
            if (4.0 == gameDto.getPrice()){
                pricestart = 100;
                priceend = 99999999;
            }
        }
        if (gameDto.getPrice() != null){
            if (0.0 == gameDto.getPrice()){
                pricestart = 0;
                priceend = 99999999;
            }
        }

        return gameDao.getList(gameDto,pricestart,priceend);
    }

    @Override
    public Long getcount(GameDto gameDto) throws SQLException {
        if (gameDto.getPrice() != null){
            if (1.0 == gameDto.getPrice()){
                pricestart = 0;
                priceend = 20;
            }
        }
        if (gameDto.getPrice() != null){
            if (2.0 == gameDto.getPrice()){
                pricestart = 20;
                priceend = 50;
            }
        }
        if (gameDto.getPrice() != null){
            if (3.0 == gameDto.getPrice()){
                pricestart = 50;
                priceend = 100;
            }
        }
        if (gameDto.getPrice() != null){
            if (4.0 == gameDto.getPrice()){
                pricestart = 100;
                priceend = 99999999;
            }
        }
        if (gameDto.getPrice() != null){
            if (0.0 == gameDto.getPrice()){
                pricestart = 0;
                priceend = 99999999;
            }
        }

        return gameDao.getCount(gameDto,pricestart,priceend);
    }

    @Override
    public Game getone(Long gameid) throws SQLException {
        return gameDao.getOne(Game.builder().id(gameid).build());
    }

    @Override
    public int addDream(Long id, Game game) throws SQLException {
        return gameDao.addDream(id, game);
    }

    @Override
    public int buy(User user, Game game, String keyword) throws SQLException {
        int result = gameDao.buy(user, game, keyword);
        if(result == 1){
            gameDao.update(user);
        }
        return result;
    }

    @Override
    public void delDream(User user, Game game) throws SQLException {
        gameDao.delDream(user, game);
    }

    @Override
    public KeyWord getKey(Long gameid) throws SQLException {
        return gameDao.getKey(gameid);
    }

    @Override
    public void updateKey(String keyword) throws SQLException {
        gameDao.updateKey(keyword);
    }

    @Override
    public void remove(Long id, Long gameid) throws SQLException {
        gameDao.remove(id,gameid);
    }
}
